//定义横屏bug问题和设置页面rem的字体大小
(function (designWidth, maxWidth) {
    var doc = document,
        win = window,
        docEl = doc.documentElement,
        remStyle = document.createElement("style"),
        tid;

    //浏览器竖屏与横屏转换的BUG。
    function refreshRem() {
        var width = docEl.getBoundingClientRect().width;
        maxWidth = maxWidth || 540;
        width > maxWidth && (width = maxWidth);
        var rem = width * 100 / designWidth;
        remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
    }

    if (docEl.firstElementChild) {
        docEl.firstElementChild.appendChild(remStyle);
    } else {
        var wrap = doc.createElement("div");
        wrap.appendChild(remStyle);
        doc.write(wrap.innerHTML);
        wrap = null;
    }
    //要等 wiewport 设置好后才能执行 refreshRem，不然 refreshRem 会执行2次；
    refreshRem();

    win.addEventListener("resize", function () {
        clearTimeout(tid); //防止执行两次
        tid = setTimeout(refreshRem, 300);
    }, false);

    win.addEventListener("pageshow", function (e) {
        if (e.persisted) { // 浏览器后退的时候重新计算
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        }
    }, false);

    if (doc.readyState === "complete") {
        doc.body.style.fontSize = "16px";
    } else {
        doc.addEventListener("DOMContentLoaded", function () {
            doc.body.style.fontSize = "16px";
        }, false);
    }
})(750, 750);
//axios  设置拦截器
axios.defaults.timeout = 30000;//请求超时
// axios.defaults.retry = 3;                 //请求次数
// axios.defaults.retryDelay = 1000;         //请求间隙
axios.interceptors.request.use(function (config) {

if (config.method === 'post') {
    var data = config.data;
    if (!data) {
        data = {
            'token': localStorage.getItem('token'),
        }
    } else {
        data['token'] = localStorage.getItem('token');
    }
    config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    };
    config.data = qs.stringify(data);
} else {
    var info = config.params;
    if (!info) {
        info = {
            'token': localStorage.getItem('token'),
            'em': 2001
        }
    } else {
        info['token'] = localStorage.getItem('token');
        info['em'] = 2001;
    }
    config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    config.params = info;
}

return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});

//封装公用函数挂载在vue上
Vue.prototype.post=function(url, data, callback,fail){
    window.console.log(url)
    window.console.log(qs.stringify(data));
    axios({
        method: 'post',
        url: url,
        data: data,
        headers: {
            'token': localStorage.getItem("token")
        },
        timeout: 30000,
    }).then(function (res) {
        callback(res.data)
    }).catch(function (error) {
        fail(error)
    });
}

Vue.prototype.get = function (url, params, callback,fail){
    axios({
        method: 'get',
        url: url,
        params: params,
        timeout: 30000,
    }).then(function (res) {
        callback(res.data)
    }).catch(function (error) {
        fail(error)
    });
}
